package com.program.facesigninsystem.util;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class HttpClient {
    private static Logger log = Logger.getLogger(HttpClient.class.toString());
    public static Map<String, Object> getWxUserOpenid(String code, String APPID, String APPSecret) {
        StringBuilder url = new StringBuilder("https://api.weixin.qq.com/sns/jscode2session?");
        url.append("appid=");//appid设置
        url.append(APPID);
        url.append("&secret=");//secret设置
        url.append(APPSecret);
        url.append("&js_code=");//code设置
        url.append(code);
        url.append("&grant_type=authorization_code");
        System.out.println(url);
        Map<String, Object> map = null;
        try {
            CloseableHttpClient client = HttpClientBuilder.create().build();//构建一个Client
            HttpGet get = new HttpGet(url.toString()); //构建一个GET请求
            CloseableHttpResponse response = client.execute(get);//提交GET请求
            HttpEntity result = response.getEntity();//拿到返回的HttpResponse的"实体"
            String content = EntityUtils.toString(result);
            System.out.println(content);//打印返回的信息
            JSONObject res = JSONObject.fromObject(content);//把信息封装为json
            //把信息封装到map
            map = HttpClient.parseJSON2Map(res);//这个小工具的代码在下面
        } catch (Exception e) {
            log.log(Level.SEVERE,"Error parsing date",e);
        }
        return map;
    }

    private static Map<String, Object> parseJSON2Map(JSONObject json) {
        // 最外层解析
        Map<String, Object> map = new HashMap<>();
        for (Object k : json.keySet()) {
            Object v = json.get(k);
        // 如果内层还是数组的话，继续解析
            if (v instanceof JSONArray) {
                List<Map<String, Object>> list = new ArrayList<>();
                for (JSONObject json2 : (Iterable<JSONObject>) v) {
                    list.add(parseJSON2Map(json2));
                }
                map.put(k.toString(), list);
            } else {
                map.put(k.toString(), v);
            }
        }
        return map;
    }
}

